//
//  CmtCollectionReusableView.m
//  Products_B
//
//  Created by lanou on 16/6/3.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "CmtCollectionReusableView.h"

@interface CmtCollectionReusableView ()
@property(nonatomic,assign)BOOL isClink;

@property(nonatomic,strong)NSString *string;

@property(nonatomic,strong)UIButton *rightButton;
@end

@implementation CmtCollectionReusableView


-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        
        //    表头
//        self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight / 2)];
        //    背景图
        self.image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10, kWidth, kWidth / 2)];
        [self addSubview:self.image];
        //    参与人数
        self.user_count = [[UILabel alloc]initWithFrame:CGRectMake(20, kWidth / 2 + 20, 100, 25)];
        self.user_count.textColor = [UIColor grayColor];
        self.user_count.font = [UIFont systemFontOfSize:14];
        self.user_count.text = @"0人参与";
        [self addSubview:self.user_count];
        //  结束倒计时
        self.end_time_left = [[UILabel alloc]initWithFrame:CGRectMake(kWidth - 180 - 20, kWidth / 2 + 20, 180, 25)];
        self.end_time_left.font = [UIFont systemFontOfSize:14];
        self.end_time_left.text = @"结束倒计时：还剩下0天";
        self.end_time_left.textColor = [UIColor grayColor];
        [self addSubview:self.end_time_left];
        
        // 内容
        self.content = [[UILabel alloc]initWithFrame:CGRectMake(20, kWidth / 2 + 50, kWidth - 40, 100)];
        self.content.numberOfLines = 0;
        self.content.textColor = [UIColor grayColor];
        self.content.font = [UIFont systemFontOfSize:14];
        [self addSubview:self.content];

//        下拉按钮
        self.button = [UIButton buttonWithType:(UIButtonTypeSystem)];
        _button.frame = CGRectMake(0, self.content.frame.size.height + kWidth / 2 + 50, kWidth, 30);
        [_button setImage:[UIImage imageNamed:@"Cmtdown.png"] forState:(UIControlStateNormal)];
        [_button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [self addSubview:_button];
//        
        self.imge1 = [[UIImageView alloc]initWithFrame:CGRectMake((kWidth - 120) / 6, _button.frame.origin.y + 30 + 20, 30, 30)];
        self.imge1.backgroundColor = [UIColor grayColor];
        self.imge1.layer.cornerRadius = 15;
        self.imge1.layer.masksToBounds = YES;
        [self addSubview:_imge1];
        
        self.imge2 = [[UIImageView alloc]initWithFrame:CGRectMake((kWidth - 120) / 6 + 40 , _button.frame.origin.y + 30 + 20, 30, 30)];
        self.imge3 = [[UIImageView alloc]initWithFrame:CGRectMake((kWidth - 120) / 6 + 80 , _button.frame.origin.y + 30 + 20, 30, 30)];
        self.imge4 = [[UIImageView alloc]initWithFrame:CGRectMake((kWidth - 120) / 6 + 120 , _button.frame.origin.y + 30 + 20, 30, 30)];
        self.imge5 = [[UIImageView alloc]initWithFrame:CGRectMake((kWidth - 120) / 6 + 160 , _button.frame.origin.y + 30 + 20, 30, 30)];
        self.imge6 = [[UIImageView alloc]initWithFrame:CGRectMake((kWidth - 120) / 6 + 200 , _button.frame.origin.y + 30 + 20, 30, 30)];
        
        
        self.rightButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
        _rightButton.frame = CGRectMake(kWidth - 50 , _button.frame.origin.y + 30 + 20, 30, 30);
        [_rightButton setImage:[UIImage imageNamed:@"CmtRight.png"] forState:(UIControlStateNormal)];
        
        [_rightButton addTarget:self action:@selector(buttonStepAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [self addSubview:_rightButton];
        
        self.imge2.backgroundColor = [UIColor grayColor];
        self.imge3.backgroundColor = [UIColor grayColor];
        self.imge4.backgroundColor = [UIColor grayColor];
        self.imge5.backgroundColor = [UIColor grayColor];

        self.imge6.backgroundColor = [UIColor grayColor];
        
        self.imge2.layer.cornerRadius = 15;
        self.imge2.layer.masksToBounds = YES;
        self.imge3.layer.cornerRadius = 15;
        self.imge3.layer.masksToBounds = YES;
        self.imge4.layer.cornerRadius = 15;
        self.imge4.layer.masksToBounds = YES;
        self.imge5.layer.cornerRadius = 15;
        self.imge5.layer.masksToBounds = YES;
        self.imge6.layer.cornerRadius = 15;
        self.imge6.layer.masksToBounds = YES;
        
        [self addSubview:_imge2];
        [self addSubview:_imge3];
        [self addSubview:_imge4];
        [self addSubview:_imge5];
        [self addSubview:_imge6];
        
        self.backgroundColor = [UIColor whiteColor];
        
    }
    return self;
}

//点击更多赞
-(void)buttonStepAction:(UIButton *)button{

    if (self.block) {
        self.block();
    }

}

//动态返回
-(void)buttonAction:(UIButton *)button{

//    NSLog(@"点击了按钮");
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName];
    CGRect bounds = [self.model.content boundingRectWithSize:CGSizeMake(kWidth - 40, 10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    
    if (!self.isClink) {
        
        [button setImage:[UIImage imageNamed:@"CmtUps.png"] forState:(UIControlStateNormal)];
        self.content.frame = CGRectMake(20, kWidth / 2 + 50, kWidth - 40, bounds.size.height);
        //        self.content.backgroundColor = [UIColor redColor];
        button.frame = CGRectMake(0, bounds.size.height + kWidth / 2 + 50, kWidth, 30);
        self.imge1.frame = CGRectMake((kWidth - 120) / 6, button.frame.origin.y + 30 + 20, 30, 30);
        self.imge2.frame = CGRectMake((kWidth - 120) / 6 + 40 , button.frame.origin.y + 30 + 20, 30, 30);
        self.imge3.frame = CGRectMake((kWidth - 120) / 6 + 80 , button.frame.origin.y + 30 + 20, 30, 30);
        self.imge4.frame = CGRectMake((kWidth - 120) / 6 + 120 , button.frame.origin.y + 30 + 20, 30, 30);
        self.imge5.frame = CGRectMake((kWidth - 120) / 6 + 160 , button.frame.origin.y + 30 + 20, 30, 30);
        self.imge6.frame = CGRectMake((kWidth - 120) / 6 + 200 , button.frame.origin.y + 30 + 20, 30, 30);
        _rightButton.frame = CGRectMake(kWidth - 50 , _button.frame.origin.y + 30 + 20, 30, 30);

        
    }
    else{
        
        [button setImage:[UIImage imageNamed:@"Cmtdown.png"] forState:(UIControlStateNormal)];
        self.content.frame = CGRectMake(20, kWidth / 2 + 50, kWidth - 40, 100);
        button.frame = CGRectMake(0, self.content.frame.size.height + kWidth / 2 + 50, kWidth, 30);
        self.imge1.frame = CGRectMake((kWidth - 120) / 6, button.frame.origin.y + 30 + 20, 30, 30);
        self.imge2.frame = CGRectMake((kWidth - 120) / 6 + 40 , button.frame.origin.y + 30 + 20, 30, 30);
        self.imge3.frame = CGRectMake((kWidth - 120) / 6 + 80 , button.frame.origin.y + 30 + 20, 30, 30);
        self.imge4.frame = CGRectMake((kWidth - 120) / 6 + 120 , button.frame.origin.y + 30 + 20, 30, 30);
        self.imge5.frame = CGRectMake((kWidth - 120) / 6 + 160 , button.frame.origin.y + 30 + 20, 30, 30);
        self.imge6.frame = CGRectMake((kWidth - 120) / 6 + 200 , button.frame.origin.y + 30 + 20, 30, 30);
        _rightButton.frame = CGRectMake(kWidth - 50 , _button.frame.origin.y + 30 + 20, 30, 30);

        
    }
    
    self.isClink = !self.isClink;
    
    NSNumber *boolNum = [NSNumber numberWithBool:self.isClink];

    [[NSNotificationCenter defaultCenter]postNotificationName:@"loadHeightofHead" object:@[boolNum,self]];

}

-(void)setModel:(CmtActModel *)model{

    if (model == nil) {
        return;
    }
    
    _model = model;
    
    [self.image sd_setImageWithURL:[NSURL URLWithString:model.image]];
    self.user_count.text = [NSString stringWithFormat:@"%@人参与",model.user_count];
    self.end_time_left.text = [NSString stringWithFormat:@"结束倒计时：还剩下%ld天",model.end_time_left.integerValue  / 24 / 3600];
    self.content.text = [NSString stringWithFormat:@"%@",model.content];
   
    if (model.modelArray.count > 0) {
        CmtSecAttenRelates *relates0 = model.modelArray[0];
        [self.imge6 sd_setImageWithURL:[NSURL URLWithString:relates0.head_img]];
        if (model.modelArray.count >1) {
            CmtSecAttenRelates *relates1 = model.modelArray[1];
            [self.imge1 sd_setImageWithURL:[NSURL URLWithString:relates1.head_img]];
        }
        if (model.modelArray.count >2) {
            CmtSecAttenRelates *relates2 = model.modelArray[2];
             [self.imge2 sd_setImageWithURL:[NSURL URLWithString:relates2.head_img]];
        }
        if (model.modelArray.count > 3) {
            
            CmtSecAttenRelates *relates3 = model.modelArray[3];
            [self.imge3 sd_setImageWithURL:[NSURL URLWithString:relates3.head_img]];

        }
        if (model.modelArray.count > 4) {
            CmtSecAttenRelates *relates4 = model.modelArray[4];
            [self.imge4 sd_setImageWithURL:[NSURL URLWithString:relates4.head_img]];

        }
        if (model.modelArray.count > 5) {
            CmtSecAttenRelates *relates5 = model.modelArray[5];
            [self.imge5 sd_setImageWithURL:[NSURL URLWithString:relates5.head_img]];
        }
    }
    
}


-(CGSize)headerViewHeight:(CmtActModel *)model{

    if (model.modelArray != 0) {
        if (self.isClink == YES) {
            
            NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName];
            CGRect bounds = [model.content boundingRectWithSize:CGSizeMake(kWidth - 40, 10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
            return CGSizeMake(kWidth, bounds.size.height + kWidth / 2 + 50 + 80);
        }
    }

    return CGSizeMake(kWidth, 100 + kWidth / 2 + 50 + 80);
}


- (void)awakeFromNib {
    // Initialization code
}

@end
